/**
 	FileUpload class
 */
package nrb.utils
{
	import constant.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	
	import mx.controls.Alert;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for File Upload
	 */
    public class FileUpload extends Sprite {
		
		/**
		 	variables for listener function, file filter, file reference, file url
		 */
		private var file:FileReference;
		private var file_filter:FileFilter;
		private var file_url:String = '';
		private var obj_listener:Object;
		private var listener:Function;
		private var variables:URLVariables;
		private var request:URLRequest;
		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables and configure listeners for file
		 */
        public function FileUpload() {
        	listener = new Function();
        	obj_listener = new Object;
            file = new FileReference();
            request = new URLRequest();
        	variables = new URLVariables();
            configureListeners(file);
        }
        
         /**
		 	Type: Public Function
		 	@Params:value['URL'] AS String
		 	@return:void
		 	Desc: sets url
		 */
        public function set_url(value:String): void {
        	request.url = value;
        }
        
        /**
		 	Type: Public Function
		 	@Params:obj[{var1:'String', var2:'String', var3:'String'}] AS Object
		 	@return:void
		 	Desc: sets parameters ready for sending to server side
		 */
        public function set_parameters(obj:Object): void {
        	for(var i:String in obj){
				variables[i] = obj[i];
			}
			//variables.test = 'jed';
			request.data = variables;
			request.method = URLRequestMethod.GET;
        }
        
        /**
		 	Type: Public Function
		 	@Params:obj[{description:"Image Files (*.jpg, *.jpeg, *.gif, *.png)", extension:"*.jpg; *.jpeg; *.gif; *.png"}] AS Object
		 	@return:void
		 	Desc: sets the file filters
		 */
        public function set_filters(obj:Object): void {
        	file_filter = new FileFilter(obj.description, obj.extension);
        }
        
        /**
		 	Type: Public Function
		 	@Params:listener[Object] AS Object
		 	@return:void
		 	Desc: browse file
		 */
        public function browse_file(obj:Object): void {
        	if(file_filter != null)
        		file.browse([file_filter]);
        	else
        		file.browse();
        		
        	listener = obj.listener;
        }
           
        /**
		 	Type: Public Function
		 	@Params:obj[{listener:Function}] AS Object
		 	@return:void
		 	Desc: uploads file
		 */
        public function upload_file(obj:Object): void {
        	listener = obj.listener;
        	//var encoder:Base64Encoder = new Base64Encoder();
        	//var username:String = 'e009379';
        	//var password:String = 'josepaolo0323';
        	//encoder.encode(username + ":" + password);
        	//var encode_credentials:String = encoder.toString();
        	//var request:URLRequest = new URLRequest(file_url);
        	//var headers:URLRequestHeader = new URLRequestHeader("WWW-Authenticate", " realm='CLS-On-RubyRails-Secured' basic-credentials=" + encode_credentials);
        	//var headers:URLRequestHeader = new URLRequestHeader("Authorization", "SSPI " + encode_credentials);
        	//var headers1:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
        	//request.requestHeaders.push(headers);
        	//request.requestHeaders.push(headers1);
        	   
        	//var request:URLRequest = new URLRequest(file_url);
        	file.upload(request);
        }
        
        /**
		 	Type: Private Function
		 	@Params:dispatcher[IEventDispatcher] AS IEventDispatcher
		 	@return:void
		 	Desc: adds listeners to file
		 */
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.SELECT, select_handler);
		    dispatcher.addEventListener(Event.OPEN, open_handler);
		    dispatcher.addEventListener(ProgressEvent.PROGRESS, progress_handler);
		    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		    dispatcher.addEventListener(Event.COMPLETE, complete_handler);
        }
        
 		/**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for selecting file
		 */
        private function select_handler(event:Event): void {
        	//obj_listener.text = event.target.name;
        	//Alert.show(event.target.size);
        	var obj:Object = new Object;
        	obj['name'] = event.target.name;
        	obj['size'] = event.target.size;
        	obj['type'] = event.target.type;
        	
        	listener(obj);
    	}
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for opening file
		 */
        private function open_handler(event:Event): void {
        	
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for progress of uploading file
		 */
        private function progress_handler(event:ProgressEvent): void {
        	
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for security error
		 */
        private function securityErrorHandler(event:SecurityErrorEvent):void {
            Alert.show("securityErrorHandler: " + event);
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for error 
		 */
        private function ioErrorHandler(event:IOErrorEvent):void {
            Alert.show("ioErrorHandler: " + event);
        }
        
        /**
		 	Type: Private Function
		 	@Params:event[Event] AS Event
		 	@return:void
		 	Desc: listener for complete uploading of file
		 */
        private function complete_handler(event:Event): void {
        	if(listener != null){
        		listener();
        	}else{
        		Alert.show('Upload Complete');
        	}
        }
       

		
		
	}
}